using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Material.Object;
using Material.Entity;
using System.Configuration;

namespace Material.App
{
    public partial class FrmPembBarangDTL : Form
    {
        private string connStr = ConfigurationManager.ConnectionStrings["dbconnection"].ToString();
        public static int idTransHDR = 0;
        public static int idBarang = 0;
        public static int idTransDTL = 0;
        public static string stateInput = string.Empty;
        public static bool isHeader=false;
        //public static int idBarang;


        //private TblTransPemBarangDTL tblTransPemBarangDTL;
        private TblTransPemBarangDTLEntity tblTransPemBarangDTLEntity;

        //private TblTransPembBarangHDR tblTransPembBarangHDR;
        private TblTransPembBarangHDREntity tblTransPembBarangHDREntity;
        public FrmPembBarangDTL()
        {
            InitializeComponent();
            if (stateInput == "Edit")
            {
                //tblTransPemBarangDTL = new TblTransPemBarangDTL();
                //tblTransPemBarangDTL.IdTransDTL = idTransDTL;
                //tblTransPemBarangDTLEntity = new TblTransPemBarangDTLEntity(connStr);
                //tblTransPemBarangDTL = tblTransPemBarangDTLEntity.GetDataByID(tblTransPemBarangDTL);
                //tglBeli.Value = Convert.ToDateTime(tblTransPemBarangDTL.TglBeli);
                //txtJumlah.Text = tblTransPemBarangDTL.Jumlah.ToString();
                //txtHarga1.Text = tblTransPemBarangDTL.Harga1.ToString();
                //txtHarga2.Text = tblTransPemBarangDTL.Harga2.ToString();
                //txtHarga3.Text = tblTransPemBarangDTL.Harga3.ToString();
                //txtTotal.Text = tblTransPemBarangDTL.TotalHarga.ToString();

            }
            else
                ClearTextBox();
        }

        private void ClearTextBox()
        {
            txtDisc1.Text = "0";
            txtDisc2.Text = "0";
            txtHarga1.Text = "0";
            txtHarga2.Text = "0";
            txtHarga3.Text = "0";
            txtJumlah.Text = "0";
            txtTotal.Text = "0";

            txtDisc1.Text = "0";
            txtDisc2.Text = "0";
            
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            ClearTextBox();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
        //    int i = 0;

        //    if (isHeader)
        //    {
        //        tblTransPembBarangHDR = new TblTransPembBarangHDR();
        //        tblTransPembBarangHDREntity = new TblTransPembBarangHDREntity(connStr);
        //        tblTransPembBarangHDR = tblTransPembBarangHDREntity.GetMaxIDTrans();
        //        tblTransPembBarangHDR.IdTrans = tblTransPembBarangHDR.IdTrans;
        //        tblTransPembBarangHDR.IdBarang = idBarang;
        //        tblTransPembBarangHDR.Created = DateTime.Now;
        //        tblTransPembBarangHDR.CreatedBy = FrmLogin.UserID;
        //        tblTransPembBarangHDR.Modified = DateTime.Now;
        //        tblTransPembBarangHDR.ModifiedBy = FrmLogin.UserID;
        //        idTransHDR = tblTransPembBarangHDR.IdTrans;
        //        FrmPembBarang.idTrans = tblTransPembBarangHDR.IdTrans;

        //        i = tblTransPembBarangHDREntity.Insert(tblTransPembBarangHDR);
        //    }
        //    if (i != -1)
        //    {
        //        tblTransPemBarangDTL = new TblTransPemBarangDTL();
        //        tblTransPemBarangDTLEntity = new TblTransPemBarangDTLEntity(connStr);

        //        tblTransPemBarangDTL.IdTransHDR = idTransHDR;
        //        tblTransPemBarangDTL.Jumlah = Convert.ToInt32(txtJumlah.Text.Trim());
        //        tblTransPemBarangDTL.Harga1 = Convert.ToDecimal(txtHarga1.Text.Trim());
        //        tblTransPemBarangDTL.Harga2 = Convert.ToDecimal(txtHarga2.Text.Trim());
        //        tblTransPemBarangDTL.Harga3 = Convert.ToDecimal(txtHarga3.Text.Trim());
        //        tblTransPemBarangDTL.TotalHarga = tblTransPemBarangDTL.Harga1 * tblTransPemBarangDTL.Jumlah; //Convert.ToDecimal(txtTotal.Text.Trim());
        //        tblTransPemBarangDTL.TglBeli = Convert.ToDateTime(tglBeli.Value);

        //        if (stateInput.Trim() == "Add")
        //        {
        //            tblTransPemBarangDTL.Created = DateTime.Now;
        //            tblTransPemBarangDTL.CreatedBy = FrmLogin.UserID;
        //            tblTransPemBarangDTL.Modified = DateTime.Now;
        //            tblTransPemBarangDTL.ModifiedBy = FrmLogin.UserID;
        //            i = tblTransPemBarangDTLEntity.Insert(tblTransPemBarangDTL);
        //        }
        //        else
        //        {
        //            tblTransPemBarangDTL.Modified = DateTime.Now;
        //            tblTransPemBarangDTL.ModifiedBy = FrmLogin.UserID;
        //            tblTransPemBarangDTL.IdTransDTL = idTransDTL;

        //            i = tblTransPemBarangDTLEntity.Update(tblTransPemBarangDTL);
        //        }

        //        if (i != -1)
        //        {
        //            MessageBox.Show("Simpan data berhasil", "Pesan", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    
        //        }
        //        else
        //            MessageBox.Show("Simpan data tidak berhasil", "Pesan", MessageBoxButtons.OK, MessageBoxIcon.Error);

        //        this.Close();
        //    }
           


           


        }

        private void FrmPembBarangDTL_Load(object sender, EventArgs e)
        {

        }

        private void txtDisc1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtHarga2.Text = Convert.ToString((Convert.ToDecimal(txtHarga1.Text) * Convert.ToInt32(txtDisc1.Text)) / 100 + Convert.ToDecimal(txtHarga1.Text));
                txtDisc2.Focus();
            }

        }

        private void txtDisc2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtHarga3.Text = Convert.ToString((Convert.ToDecimal(txtHarga1.Text) * Convert.ToInt32(txtDisc2.Text)) / 100 + Convert.ToDecimal(txtHarga1.Text));
            }
        }

        private void txtJumlah_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtHarga1.Focus();
                //txtHarga2.Text = Convert.ToString((Convert.ToDecimal(txtHarga1.Text) * Convert.ToInt32(txtDisc1.Text)) / 100 + Convert.ToDecimal(txtHarga1.Text));
            }
        }

        private void txtJumlah_KeyPress(object sender, KeyPressEventArgs e)
        {
            //Using Regular Expressions
            if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+"))
                e.Handled = true;
        }

        private void txtHarga1_KeyPress(object sender, KeyPressEventArgs e)
        {
            //Using Regular Expressions
            if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+"))
                e.Handled = true;
        }

        private void txtDisc1_KeyPress(object sender, KeyPressEventArgs e)
        {
            //Using Regular Expressions
            if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+"))
                e.Handled = true;
        }

        private void txtDisc2_KeyPress(object sender, KeyPressEventArgs e)
        {
            //Using Regular Expressions
            if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+"))
                e.Handled = true;
        }

        private void txtDisc1_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtJumlah_TextChanged(object sender, EventArgs e)
        {

        }
    }
}